package com.springspirit.monitor.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.springspirit.framework.exception.AuthenticationException;
import com.springspirit.framework.exception.BizException;
import com.springspirit.framework.exception.EpSsoException;
import com.springspirit.framework.jms.JmsQueueSender;
import com.springspirit.framework.logging.LogAppender;
import com.springspirit.framework.message.LogMessage;

/**
 * 서버 로그 메시지를 제너레이팅한다.
 * 
 * @author greatshin
 *
 */
@Controller
public class LogGeneratorController {

	private static Logger logger = Logger.getLogger(LogGeneratorController.class);
	
	@Inject
    @Named("jmsQueueSender")
	JmsQueueSender jmsQueueSender;
	
	@Autowired
	LogAppender logAppender;
	
	@RequestMapping("/logGen.do")
	public ModelAndView logGen2(HttpServletRequest req) throws Exception{
		
		List logMessageList = new ArrayList();
//		
//		LogMessage logMessage = makeLogMessage(0, "BizException");
//		
//		logger.debug("[JMS] Message Sended!: " + logMessage.toString());
//
//		jmsQueueSender.send(logMessage);
//		
//		logMessageList.add(logMessage);
		
//		for(int jj=0; jj<20; jj++){
			
			for (int i = 0; i < 5; i++) {
				LogMessage logMessage = makeLogMessage(i, "Critical");
	
				logger.debug("[JMS] Message Sended!: " + logMessage.toString());
	
				jmsQueueSender.send(logMessage);
				
				logAppender.insertLog(logMessage);
				
				logMessageList.add(logMessage);
				
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
//				throw new EpSsoException(logMessage);
			}
			
//			for (int i = 100; i < 200; i++) {
//				LogMessage logMessage = makeLogMessage(i, "Error");
//	
//				logger.debug("[JMS] Message Sended!: " + logMessage.toString());
//	
//				jmsQueueSender.send(logMessage);
//				
//				logMessageList.add(logMessage);
//				
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				
////				throw new com.springspirit.framework.exception.SecurityException(logMessage);
//			}
//			
//			for (int i = 200; i < 300; i++) {
//				LogMessage logMessage = makeLogMessage(i, "Warning");
//	
//				logger.debug("[JMS] Message Sended!: " + logMessage.toString());
//	
//				jmsQueueSender.send(logMessage);
//				
//				logMessageList.add(logMessage);
//				
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				
////				throw new AuthenticationException(logMessage);
//			}
//			
//			for (int i = 300; i < 400; i++) {
//				LogMessage logMessage = makeLogMessage(i, "Warning");
//	
//				logger.debug("[JMS] Message Sended!: " + logMessage.toString());
//	
//				jmsQueueSender.send(logMessage);
//				
//				logMessageList.add(logMessage);
//				
//				try {
//					Thread.sleep(100);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				
////				throw new EpSsoException(logMessage);
//			}
//		}
		
		ModelAndView mnv = new ModelAndView("logGen");
		mnv.addObject("logMessageList", logMessageList);
		mnv.addObject("count", logMessageList.size());
		mnv.addObject("hello", "hello");
		
		return mnv;
	}

	private LogMessage makeLogMessage(int i, String errorCategory) {

		UUID uid = UUID.randomUUID();
		
		String id = uid.toString();
		String loginUser = "greatshin2";
		String loginIpAddress = "127.0.0.1";
		String subSystem = "common";
		String menu = "dp";
		String responsePerson = "greatshin2@gmail.com";
		String layer = "service";
//		String errorCategory = "BizException";
		String errorMessage = 		
			"java.sql.SQLException: org.apache.tomcat.dbcp.dbcp.DelegatingStatement with address: 'oracle.jdbc.driver.T4CStatement@14e6be2' is closed." +
			"at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.checkOpen(DelegatingStatement.java:137)" +
			"at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.cancel(DelegatingStatement.java:252)" +
			"at mondrian.server.Execution.cleanStatements(Execution.java:147)" +
			"at mondrian.server.Execution.end(Execution.java:168)" +
			"at mondrian.server.StatementImpl.end(StatementImpl.java:98)" +
			"at mondrian.rolap.RolapConnection.executeInternal(RolapConnection.java:644)" +
			"at mondrian.rolap.RolapConnection.access$000(RolapConnection.java:52)" +
			"at mondrian.rolap.RolapConnection$1.call(RolapConnection.java:586)" +
			"at mondrian.rolap.RolapConnection$1.call(RolapConnection.java:584)" +
			"at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)" +
			"at java.util.concurrent.FutureTask.run(FutureTask.java:138)" +
			"at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)" +
			"at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)" +
			"at java.lang.Thread.run(Thread.java:662)";
		
		DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSS");		
		String timeStamp = String.valueOf(formatter.format(System.currentTimeMillis()));

		LogMessage logMessage = new LogMessage(id, Integer.toString(i), loginUser, loginIpAddress,
				subSystem, menu, responsePerson, layer, errorCategory,
				errorMessage, timeStamp);

		return logMessage;
	}
	
}
